package edu.mtu.citizenscience.ethnography.util;

import java.util.Locale;

import edu.mtu.citizenscience.ethnography.R;
import edu.mtu.citizenscience.ethnography.uploader.EthnographyUploadService;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class UploadDialogFragment extends DialogFragment {
	private static final String LOG_TAG = "UploadDialogFragment";
	public static final String FRAG_TAG = LOG_TAG;
	
	public static final String ID = "id";
	
	private LocalBroadcastManager manager;
	private BroadcastReceiver receiver;
	private IntentFilter filter;
	
	private TextView text;
	private ProgressBar progressBar;
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		int id = getArguments().getInt(ID, -1);
		
		Intent intent = new Intent(getActivity(), EthnographyUploadService.class);
		intent.putExtra(EthnographyUploadService.INPUT_METHOD, EthnographyUploadService.METHOD_UPLOAD);
		intent.putExtra(EthnographyUploadService.INPUT_ID, id);
		
		builder.setView(inflater.inflate(R.layout.dialog_progress, null));
		builder.setPositiveButton(R.string.uploadOK, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				getDialog().dismiss();
			}
		});
		
		AlertDialog dialog = builder.show();
		dialog.getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.GONE);
		text = (TextView) dialog.findViewById(R.id.statusText);
		progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar);
		
		manager = LocalBroadcastManager.getInstance(getActivity());
		filter = new IntentFilter(EthnographyUploadService.UPLOAD_SERVICE);
		receiver = new UploadBroadcastReceiver();
		manager.registerReceiver(receiver, filter);
		
		getActivity().startService(intent);
		
		return dialog;
	}
	
	@Override
	public void onStop() {
		if (receiver != null) {
			manager.unregisterReceiver(receiver);
		}
		super.onStop();
	}
	
	private class UploadBroadcastReceiver extends BroadcastReceiver {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			String status = intent.getStringExtra(EthnographyUploadService.OUTPUT_RESULT);
			if (status == EthnographyUploadService.RESULT_SUCCESS) {
				// Success; remove the dialog
				manager.unregisterReceiver(receiver);
				receiver = null;
				getDialog().dismiss();
			} else if (status == EthnographyUploadService.RESULT_CONTINUE) {
				// In progress; keep going
				String sendType = intent.getStringExtra(EthnographyUploadService.CONTINUE_SENDTYPE);
				if (sendType == EthnographyUploadService.SENDTYPE_INTERVIEW) {
					text.setText(R.string.uploadingStory);
				} else if (sendType == EthnographyUploadService.SENDTYPE_AUDIO) {
					String str = getString(R.string.uploadingAudio);
					int current = intent.getIntExtra(EthnographyUploadService.CONTINUE_CURRENT, -1);
					int total = intent.getIntExtra(EthnographyUploadService.CONTINUE_TOTAL, -1);
					String displayStr = String.format(Locale.getDefault(), "%s %d/%d", str, current, total);
					text.setText(displayStr);
				} else if (sendType == EthnographyUploadService.SENDTYPE_PHOTO) {
					String str = getString(R.string.uploadingPhoto);
					int current = intent.getIntExtra(EthnographyUploadService.CONTINUE_CURRENT, -1);
					int total = intent.getIntExtra(EthnographyUploadService.CONTINUE_TOTAL, -1);
					String displayStr = String.format(Locale.getDefault(), "%s %d/%d", str, current, total);
					text.setText(displayStr);
				}
			} else {
				// Display a message and show the OK button
				manager.unregisterReceiver(receiver);
				receiver = null;
				text.setText(R.string.uploadingError);
				progressBar.setVisibility(View.GONE);
				((AlertDialog)getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
			}
		}
		
	}
}
